#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/10/29 22:25
# @Author  : Nabby
# @File    : user_impl.py.py
# @Desc    :

from app import db
import uuid
from infrastructure.po.mode.role import Role
from infrastructure.po.mode.role_resource import RoleResource
from infrastructure.po.mode.resource import Resource

class RoleImpl(object):
    @classmethod
    def get_role_list(cls, name=None, page=1, page_size=999):
        query = db.session.query(Role)
        if name:
            query = query.filter(
                Role.name.like(f"%{name}%")
            )
        return query.paginate(page=page, per_page=page_size).items, query.count()

    @classmethod
    def get_role_by_id(cls, role_id):
        role = db.session.query(Role).filter(Role.id == role_id).first()
        return role

    @classmethod
    def add_role(cls, name, description):
        count = db.session.query(Role).filter(
            Role.name == name,
        ).count()
        if count == 0:
            role = Role()
            role.id = str(uuid.uuid4())
            role.name = name
            role.description = description
            role.status = 1
            db.session.add(role)
            db.session.flush()
            db.session.commit()
            return True
        else:
            return False

    @classmethod
    def update_role(cls, role_id, name, description):
        count = db.session.query(Role).filter(
            Role.name == name,
            Role.id != role_id,
        ).count()
        if count == 0:
            db.session.query(Role).filter(Role.id == role_id).update(
                {
                    "name": name,
                    "description": description,
                }
            )
            db.session.commit()
            return True
        else:
            return False

    @classmethod
    def change_role_state(cls, role_id, state):
        db.session.query(Role).filter(Role.id == role_id).update(
            {
                "status": state
            }
        )
        db.session.commit()
        return True

    @classmethod
    def delete_role(cls, role_id):
        count = db.session.query(Role).filter(Role.id == role_id).count()
        if count > 0:
            # 1. 先清空子表
            db.session.query(RoleResource).filter(RoleResource.role_id == "1").delete()
            # 2. 再删父表
            db.session.query(Role).filter(Role.id == role_id).delete()
            db.session.commit()
            db.session.flush()
            return True
        else:
            return False


    @classmethod
    def get_menu_tree(cls):
        query = db.session.query(Resource)
        first_menu = query.filter(
            Resource.sys_resource_id == None,
            Resource.sys_resource_type_id == 1,
            Resource.status == 1
        ).all()
        second_menu = query.filter(
            Resource.sys_resource_id != None,
            Resource.sys_resource_type_id == 2,
            Resource.status == 1
        ).all()
        return first_menu, second_menu

    # @classmethod
    # def get_menu_tree(cls):
    #     resource_first = db


